# -*- coding: utf-8 -*-
#
from .helpers import untangle2


class WandzuraXiao(object):
    '''
    S. Wandzura, H. Xiao,
    Symmetric quadrature rules on a triangle,
    Computers & Mathematics with Applications,
    Volume 45, Issue 12, June 2003, Pages 1829-1840,
    <https://doi.org/10.1016/S0898-1221(03)90004-6>.

    Abstract:
    We present a class of quadrature rules on triangles in R2 which, somewhat
    similar to Gaussian rules on intervals in R1, have rapid convergence,
    positive weights, and symmetry. By a scheme combining simple group theory
    and numerical optimization, we obtain quadrature rules of this kind up to
    the order 30 on triangles. This scheme, essentially a formalization and
    generalization of the approach used by Lyness and Jespersen over 25 years
    ago, can be easily extended to other regions in R2 and surfaces in higher
    dimensions, such as squares, spheres. We present example formulae and
    relevant numerical results.

    Note that in the above article, the authors present the coordinates in the
    symmetric triangle [[-0.5, -sqrt(3)/2], [-0.5, +sqrt(3)/2], [1, 0]]. These
    have been transformed to barycentric coordinates here.
    '''
    def __init__(self, index):
        self.name = 'WX(%d)' % index
        if index == 1:
            self.degree = 5
            data = {
                's3': [[0.2250000000000000E+00]],
                's2': [
                    [0.13239415278850623+00, 4.70142064105115109474e-01],
                    [0.12593918054482713+00, 1.01286507323456370644e-01],
                    ],
                }
        elif index == 2:
            self.degree = 10
            data = {
                's3': [[0.8352339980519638E-01]],
                's2': [
                    [0.7229850592056743E-02, 4.978654329544749e-01],
                    [0.7449217792098051E-01, 4.280124497290562e-01],
                    [0.7864647340310853E-01, 1.847564127432246e-01],
                    [0.6928323087107504E-02, 2.048121857167755e-02],
                    ],
                's1': [
                    [0.2951832033477940E-01, 1.365735762560334e-01, 3.500298989727198e-02],
                    [0.3957936719606124E-01, 3.327436005886387e-01, 3.754907025844267e-02],
                    ],
                }
        elif index == 3:
            self.degree = 15
            data = {
                's2': [
                    [0.3266181884880529E-01, 4.582807963691251e-01],
                    [0.2741281803136436E-01, 4.036104645791306e-01],
                    [0.2651003659870330E-01, 2.931971679130254e-01],
                    [0.2921596213648611E-01, 1.464677869427729e-01],
                    [0.1058460806624399E-01, 5.636286766560340e-02],
                    [0.3614643064092035E-02, 1.657512685837032e-02],
                    ],
                's1': [
                    [0.8527748101709436E-02, 2.395345541547944e-01, 9.912203309224784e-03],
                    [0.1391617651669193E-01, 4.048788073183400e-01, 1.580377063022801e-02],
                    [0.4291932940734835E-02, 9.500211311304484e-02, 5.143608816970661e-03],
                    [0.1623532928177489E-01, 1.497531073222740e-01, 4.892232575298879e-02],
                    [0.2560734092126239E-01, 2.869196124413350e-01, 6.876874863251921e-02],
                    [0.3308819553164567E-01, 2.818356680990846e-01, 1.684044181246992e-01],
                    ],
                }
        elif index == 4:
            self.degree = 20
            data = {
                's3': [[0.2761042699769952E-01]],
                's2': [
                    [0.1779029547326740E-02, 4.992496753377855e-01],
                    [0.2011239811396117E-01, 4.529301240305246e-01],
                    [0.2681784725933157E-01, 3.977639379552368e-01],
                    [0.2452313380150201E-01, 2.645002025327873e-01],
                    [0.1639457841069539E-01, 2.110189640920768e-01],
                    [0.1479590739864960E-01, 1.077356071712713e-01],
                    [0.4579282277704251E-02, 3.906908783780266e-02],
                    [0.1651826515576217E-02, 1.117437972932958e-02],
                    ],
                's1': [
                    [0.2349170908575584E-02, 6.354966590835220e-02, 5.349618187337257e-03],
                    [0.4465925754181793E-02, 1.571069189407070e-01, 7.954817066198938e-03],
                    [0.6099566807907972E-02, 3.956421143643741e-01, 1.042239828126379e-02],
                    [0.6891081327188203E-02, 2.731675707129105e-01, 1.096441479612335e-02],
                    [0.7997475072478163E-02, 1.017853824850171e-01, 3.856671208546233e-02],
                    [0.7386134285336024E-02, 4.466585491764138e-01, 3.558050781721819e-02],
                    [0.1279933187864826E-01, 1.990107941495031e-01, 4.967081636276412e-02],
                    [0.1725807117569655E-01, 3.242611836922827e-01, 5.851972508433175e-02],
                    [0.1867294590293547E-01, 2.085313632101329e-01, 1.214977870043942e-01],
                    [0.2281822405839526E-01, 3.231705665362575e-01, 1.407108449439387e-01],
                    ],
                }
        elif index == 5:
            self.degree = 25
            data = {
                's2': [
                    [0.8005581880020417E-02, 4.860267584634129e-01],
                    [0.1594707683239050E-01, 4.344106993361743e-01],
                    [0.1310914123079553E-01, 3.898891352439638e-01],
                    [0.1958300096563562E-01, 2.984432340198044e-01],
                    [0.1647088544153727E-01, 2.340441723373718e-01],
                    [0.8547279074092100E-02, 1.514683346090176e-01],
                    [0.8161885857226492E-02, 1.127338935459936e-01],
                    [0.6121146539983779E-02, 7.771569209152626e-02],
                    [0.2908498264936665E-02, 3.489309361429704e-02],
                    [0.6922752456619963E-03, 7.258184620932340e-03],
                    ],
                's1': [
                    [0.1248289199277397E-02, 2.272144521533641e-01, 1.292352704442205e-03],
                    [0.3404752908803022E-02, 4.350105548535717e-01, 5.399701272116197e-03],
                    [0.3359654326064051E-02, 3.203095992722045e-01, 6.384003033975016e-03],
                    [0.1716156539496754E-02, 9.175032228000519e-02, 5.028211501993063e-03],
                    [0.1480856316715606E-02, 3.801083585872434e-02, 6.826758621781874e-03],
                    [0.3511312610728685E-02, 1.574252184853117e-01, 1.001619963992954e-02],
                    [0.7393550149706484E-02, 2.398896597785332e-01, 2.575781317338999e-02],
                    [0.7983087477376558E-02, 3.619431181260605e-01, 3.022789811991581e-02],
                    [0.4355962613158041E-02, 8.355196095482852e-02, 3.050499010716208e-02],
                    [0.7365056701417832E-02, 1.484432207324181e-01, 4.595654736256934e-02],
                    [0.1096357284641955E-01, 2.837397087275350e-01, 6.744280054027756e-02],
                    [0.1174996174354112E-01, 4.068993751187875e-01, 7.004509141591060e-02],
                    [0.1001560071379857E-01, 1.941139870248925e-01, 8.391152464011660e-02],
                    [0.1330964078762868E-01, 3.241343470007031e-01, 1.203755356771527e-01],
                    [0.1415444650522614E-01, 2.292774835559810e-01, 1.480668991573667e-01],
                    [0.1488137956116801E-01, 3.256181225959837e-01, 1.917718658673251e-01],
                    ],
                }
        else:
            assert index == 6
            self.degree = 30
            data = {
                's3': [[0.1557996020289920E-01]],
                's2': [
                    [0.3177233700534134E-02, 4.963349417836173e-01],
                    [0.1048342663573077E-01, 4.585021620985177e-01],
                    [0.1320945957774363E-01, 4.245095219372948e-01],
                    [0.1497500696627150E-01, 3.820470700539167e-01],
                    [0.1498790444338419E-01, 2.809878457960759e-01],
                    [0.1333886474102166E-01, 2.273489758540344e-01],
                    [0.1088917111390201E-01, 1.745591115087298e-01],
                    [0.8189440660893461E-02, 1.232584272014366e-01],
                    [0.5575387588607785E-02, 8.008422889219684e-02],
                    [0.3191216473411976E-02, 4.777446740789878e-02],
                    [0.1296715144327045E-02, 2.172051468014151e-02],
                    [0.2982628261349172E-03, 4.764677615437014e-03],
                    ],
                's1': [
                    [0.9989056850788964E-03, 4.152952709133117e-01, 9.253711933464581e-04],
                    [0.4628508491732533E-03, 6.118990978534904e-02, 1.385925855563926e-03],
                    [0.1234451336382413E-02, 1.649086901369066e-01, 3.682415455910748e-03],
                    [0.5707198522432062E-03, 2.503506223200247e-02, 3.903223424159349e-03],
                    [0.1126946125877624E-02, 3.060644651510957e-01, 3.233248155010545e-03],
                    [0.1747866949407337E-02, 1.070732837302181e-01, 6.467432112236475e-03],
                    [0.1182818815031657E-02, 2.299575493455843e-01, 3.247475491332678e-03],
                    [0.1990839294675034E-02, 3.370366333057829e-01, 8.675090806753808e-03],
                    [0.1900412795035980E-02, 5.625657618206073e-02, 1.559702646731387e-02],
                    [0.4498365808817451E-02, 4.024513752124010e-01, 1.797672125368521e-02],
                    [0.3478719460274719E-02, 2.436547020108285e-01, 1.712424535388934e-02],
                    [0.4102399036723953E-02, 1.653895856145327e-01, 2.288340534658188e-02],
                    [0.4021761549744162E-02, 9.930187449584685e-02, 3.273759728776667e-02],
                    [0.6033164660795066E-02, 3.084783330690551e-01, 3.382101234234092e-02],
                    [0.3946290302129598E-02, 4.606683185921130e-01, 3.554761446001527e-02],
                    [0.6644044537680268E-02, 2.188152994539296e-01, 5.053979030686654e-02],
                    [0.8254305856078458E-02, 3.792095515602741e-01, 5.701471491573221e-02],
                    [0.6496056633406411E-02, 1.429608194181854e-01, 6.415280642120340e-02],
                    [0.9252778144146602E-02, 2.837312821059250e-01, 8.050114828762560e-02],
                    [0.9164920726294280E-02, 1.967374410044408e-01, 1.043670681345305e-01],
                    [0.1156952462809767E-01, 3.558891412116621e-01, 1.138448944287513e-01],
                    [0.1176111646760917E-01, 2.598186853519115e-01, 1.453634877155237e-01],
                    [0.1382470218216540E-01, 3.219231812312984e-01, 1.899456528219787e-01],
                    ]
                }

        self.bary, self.weights = untangle2(data)
        self.points = self.bary[:, 1:]
        return
